package com.youthfilmic.yuejian.dao;

import com.youthfilmic.yuejian.domain.Subject;
import com.youthfilmic.yuejian.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

public interface UserDao extends CrudRepository<User, Long> {

    /**
     * 根据openId获取用户
     * */
    User findByOpenId(String openId);

    Page<User> findAll(Pageable pageable);

    @Modifying
    @Transactional
    @Query("update User user set user.metCount = user.metCount + ?2 where user.id = ?1")
    void incMetCount( Long id , int inc);

    @Modifying
    @Transactional
    @Query("update User user set user.commentCount = user.commentCount + ?2 where user.id = ?1")
    void incCommentCount( Long id , int inc);

    @Modifying
    @Transactional
    @Query("update User user set user.subjectCount = user.subjectCount + ?2 where user.id = ?1")
    void incSubjectCount( Long id , int inc);

    @Modifying
    @Transactional
    @Query("update User user set user.unionId = ?2 , user.openIdFrom = ?3 where user.id = ?1")
    void updateUnionIdAndOpenIdFrom( Long id , String unionId , int openIdFrom);
}
